<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}
include 'topmenu.php';

$showForm=1;

if (!isset($FormError))
{
	$i=0;
	$FormError=array();
}

if (isset($act) && ($act==1 || $act==2))
{
	if ($act==1){$ActionUrl=1;  $ActionButton=_ADDNEW_;}
	if ($act==2){$ActionUrl=2;  $ActionButton=_EDITE_; $midActionUrl='&mid='.$mid;}

	if ($act==1 || $act==2)
	{
		$queryModuleCheck=mysql_query("SELECT modules_key FROM ".$PortalPref.$amk." WHERE modules_id='".$mid."'");
		if (mysql_num_rows($queryModuleCheck)!=0 && $act==1)
		{
			$FormError[$i]=_MODULE_EXIST_;
			$i++;
		}elseif(mysql_num_rows($queryModuleCheck)==0 && $act==2)
		{
			$FormError[$i]=_NO_MODULE_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
		}else
		{
			if ($act==2 && (!isset($mid) || !is_numeric($mid)))
			{
				$FormError[$i]=_NO_MODULE_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
			}

			if (!isset($mversion) || $mversion=='')
			{
				$FormError[$i]=_NO_MODULE_VERSION_;
				$i++;
			}
			if (!isset($mmodule) || $mmodule=='')
			{
				$FormError[$i]=_NO_MODULE_KEY_;
				$i++;
			}else
				$mmodule=trim($mmodule);
			if (!isset($muser) || $muser=='')
			{
				$FormError[$i]=_NO_MODULE_USER_NAME_;
				$i++;
			}else
				$muser=trim($muser);

			if (!isset($madmin) || $madmin=='')
			{
				$FormError[$i]=_NO_MODULE_ADMIN_NAME_;
				$i++;
			}else
				$madmin=trim($madmin);

			if (!isset($muser_right) || $muser_right==-1)
			{
				$FormError[$i]=_NO_MODULE_USER_RIGHT_;
				$i++;
			}
			if (!isset($madmin_right) || $madmin_right==-1)
			{
				$FormError[$i]=_NO_MODULE_ADMIN_RIGHT_;
				$i++;
			}

			if ($act==1)
			{
				if (isset($mtonav_flag) && $mtonav_flag==1)
				{
					if (!isset($nparent) || $nparent<0 || !is_numeric($nparent))
					{
						$FormError[$i]=_NO_PARENT_KEY_;
						$i++;
					}else
					{
						$queryGetMaxOrder=mysql_query("SELECT MAX(nav_order) AS max FROM ".$PortalPref."navigation WHERE nav_parent_id='".$nparent."'");
						$resultGetMaxOrder=mysql_fetch_assoc($queryGetMaxOrder);
						if(is_null($resultGetMaxOrder['max']))
							$orderChild=0;
						else
							$orderChild=$resultGetMaxOrder['max']+1;

						$queryToNav="INSERT INTO ".$PortalPref."navigation
						(
							nav_id,
							nav_title,
							nav_parent_id,
							nav_mkey,
							nav_admin,
							nav_module,
							nav_page,
							nav_page_position,
							nav_footer,
							nav_order,
							nav_right,
							nav_activity
						)VALUES
						(
							NULL,
							'".$muser."',
							'".$nparent."',
							'portal',
							'0',
							'".$mmodule."',
							'',
							'both',
							'0',
							'".$orderChild."',
							'".$muser_right."',
							'1'
						)";
					}
				}
			}

			if (empty($FormError))
			{
				if (!isset($mu_activity) || empty($mu_activity) || $mu_activity=='')
					$u_active=0;
				else
					$u_active=1;

				if (!isset($ma_activity) || empty($ma_activity) || $ma_activity=='')
					$a_active=0;
				else
					$a_active=1;

				if (!isset($msystem_flag) || empty($msystem_flag) || $msystem_flag=='')
					$sys_flag=0;
				else
					$sys_flag=1;

				if ($act==1)
				{
					$queryModule="INSERT INTO ".$PortalPref.$amk."
						(
							modules_id,
							modules_version,
							modules_key,
							modules_u_name,
							modules_a_name,
							modules_u_right,
							modules_a_right,
							modules_u_active,
							modules_a_active,
							modules_system
						)VALUES
						(
							NULL,
							'".$mversion."',
							'".$mmodule."',
							'".$muser."',
							'".$madmin."',
							'".$muser_right."',
							'".$madmin_right."',
							'".$u_active."',
							'".$a_active."',
							'".$sys_flag."'
						)";

						$GetChangebleSettings=getChangebleSettings($mmodule);
				}elseif($act==2)
				{
					$queryModule="UPDATE ".$PortalPref.$amk."
					SET
						modules_version='".$mversion."',
						modules_key='".$mmodule."',
						modules_u_name='".$muser."',
						modules_a_name='".$madmin."',
						modules_u_right='".$muser_right."',
						modules_a_right='".$madmin_right."',
						modules_u_active='".$u_active."',
						modules_a_active='".$a_active."',
						modules_system='".$sys_flag."'
					WHERE
						modules_id='".$mid."'
					";

					if ($old_module!=$mmodule)
					{
						$queryUpdateNav="UPDATE ".$PortalPref."navigation SET nav_module='".$mmodule."' WHERE nav_module='".$old_module."'";
						$queryUpdateOptions="UPDATE ".$PortalPref."options SET option_mkey='".$mmodule."' WHERE option_mkey='".$old_module."'";
					}
					else
					{
						$queryUpdateNav=""; $queryUpdateOptions="";
					}

					$nextStep=true;
				}

				if (mysql_query($queryModule))
				{
					if ($act==1)
					{
						$LastInsertId=mysql_insert_id();
						if (!mysql_query($GetChangebleSettings))
						{
							mysql_query("DELETE FROM ".$PortalPref.$amk." WHERE modules_id='".$LastInsertId."'");
							$FormError[$i]=_BAD_SQL_SETTINGS_;
							$i++;
							$nextStep=false;
						}else
						{
							mysql_query($queryToNav);
							$nextStep=true;
						}
					}

					if($act==2 && $queryUpdateNav!='')
					{
						mysql_query($queryUpdateNav);
						mysql_query($queryUpdateOptions);

						$avialable_langs = explode(",", $option['avialable_langs']);
						for($i=0; $i<count($avialable_langs); $i++)
						{
							mysql_query("UPDATE ".$PortalPref."langs_".$avialable_langs[$i]." SET lang_module='".$mmodule."' WHERE lang_module='".$old_module."'");
						}
					}

					if($nextStep)
					{
						$showForm=0;
						print '<p align="center">'._OPERATION_SUCCESS_.'</p>';
						echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk."'>";
					}
					//exit;
				}else
				{
					$FormError[$i]=_BAD_SQL_;
					$i++;
				}
			}
		}
	}
}elseif(isset($act) && $act==3)
{
	$ActionUrl=2;  $ActionButton=_EDITE_;
	$queryModuleSelect=mysql_query("SELECT * FROM ".$PortalPref.$amk." WHERE modules_id='".$mid."'");
	if (mysql_num_rows($queryModuleSelect)!=1)
	{
		$FormError[$i]=_NO_MODULE_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";

	}else
	{
		while($resultModuleSelect=mysql_fetch_assoc($queryModuleSelect))
		{
			$mversion=$resultModuleSelect['modules_version'];
			$mmodule=$resultModuleSelect['modules_key'];
			$muser=$resultModuleSelect['modules_u_name'];
			$madmin=$resultModuleSelect['modules_a_name'];
			$muser_right=$resultModuleSelect['modules_u_right'];
			$madmin_right=$resultModuleSelect['modules_a_right'];
			$mu_activity=$resultModuleSelect['modules_u_active'];
			$ma_activity=$resultModuleSelect['modules_a_active'];
			$msystem_flag=$resultModuleSelect['modules_system'];
		}
		$midActionUrl='&mid='.$mid;
	}
}else
{
	$ActionUrl=1;  $ActionButton=_ADDNEW_;
}

print '<p>';
getErrorList($FormError);
print '</p>';

if (isset($mu_activity) && $mu_activity==1)
	$u_activity_flag='checked';
else
	$u_activity_flag='';

if (isset($ma_activity) && $ma_activity==1)
	$a_activity_flag='checked';
else
	$a_activity_flag='';

if (isset($msystem_flag) && $msystem_flag==1)
	$sys_flag='checked';
else
	$sys_flag='';

if($ActionUrl==1)
{
	if (isset($mtonav_flag) && $mtonav_flag==1)
		$tonav_flag='checked';
	else
		$tonav_flag='';

}

if ($showForm==1)
{
	print '<form method="post" name="ModuleFrm" action="'.$portal_url.'/admin.php?amk='.$amk.'&apk='.$apk.'&act='.$ActionUrl.''.$midActionUrl.'">
		<table width="80%" align="center">
			<tr>
				<td>'._VERSION_.'</td>
				<td><input type="text" name="mversion" value="'.$mversion.'"></td>
			</tr>
			<tr>
				<td>'._MODULES_MODULE_.'</td>
				<td>';
					if ($ActionUrl==2)
					{
						if ($act==3)
							$old_module=$mmodule;
						elseif($act==2)
							$old_module=$old_module;
						print '<input type="hidden" name="old_module" value="'.$old_module.'">';
					}
				print'<input type="text" name="mmodule" value="'.$mmodule.'"></td>
			</tr>
			<tr>
				<td>'._MODULES_USER_NAME_.'</td>
				<td><input type="text" name="muser" size="15" value="'.$muser.'"></td>
			</tr>
			<tr>
				<td>'._MODULES_ADMIN_NAME_.'</td>
				<td><input type="text" name="madmin" value="'.$madmin.'"></td>
			</tr>
			<tr>
				<td>'._MODULES_USER_RIGHT_.'</td>
				<td>
					<select name="muser_right">
						<option value="-1">'._SELECT_RIGHT_.'</option>
						'.GetRightList($muser_right).'
					</select>
				</td>
			</tr>
			<tr>
				<td>'._MODULES_ADMIN_RIGHT_.'</td>
				<td>
					<select name="madmin_right">
						<option value="-1">'._SELECT_RIGHT_.'</option>
						'.GetRightList($madmin_right).'
					</select>
				</td>
			</tr>
			<tr>
				<td>'._MODULES_USER_ACTIVITY_.'</td>
				<td><input type="checkbox" name="mu_activity" value="1" '.$u_activity_flag.'></td>
			</tr>
			<tr>
				<td>'._MODULES_ADMIN_ACTIVITY_.'</td>
				<td><input type="checkbox" name="ma_activity" value="1" '.$a_activity_flag.'></td>
			</tr>
			<tr>
				<td>'._SYS_MODULE_TITLE_.'</td>
				<td><input type="checkbox" name="msystem_flag" value="1" '.$sys_flag.'></td>
			</tr>';
			if ($ActionUrl==1)
			{
				$queryNavMainForManage=mysql_query("SELECT * FROM ".$PortalPref."navigation ORDER BY nav_order ASC");
				$MenuArrayForManage = getMenuArrayAdmin($queryNavMainForManage);
				$selectedRoot='';
				if ($nparent==0)
					$selectedRoot='selected';

				print'<tr>
					<td>'._ADD_TO_NAV_.'</td>
					<td><input type="checkbox" name="mtonav_flag" value="1" '.$tonav_flag.' id="mtonav_flag" onchange="activateNav(this);">';
						print'<select name="nparent" disabled id="mNparent">
							<option value="-1">'._SELECT_PARENT_.'</option>
							<option value="0" '.$selectedRoot.'>'._ROOT_POSITION_.'</option>
							'.getMenuTreeForManage($MenuArrayForManage, 0, 0, $nparent).'
						</select>';
					print'</td>
				</tr>';
			}
			print'<tr>
				<td colspan="2"><input type="submit" value="'.$ActionButton.'"></td>
			</tr>
		</table>
	</form>';
}

function getChangebleSettings($module)
{
	global $PortalPref;
	$SettingsQuery='';
	$query=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_mkey='index' AND option_requred='1'");
	if (mysql_num_rows($query)>0)
	{
		$SettingsQuery="INSERT INTO ".$PortalPref."options
			(
				option_id,
				option_mkey,
				option_name,
				option_parametr,
				option_value,
				option_permissible,
				option_requred,
				option_type,
				option_sys
			)VALUES ";
		while($result=mysql_fetch_assoc($query))
		{
			$SettingsQuery.=
			"(
				NULL,
				'".$module."',
				'".$result['option_name']."',
				'".$result['option_parametr']."',
				'".$result['option_value']."',
				'".$result['option_permissible']."',
				'1',
				'".$result['option_type']."',
				'".$result['option_sys']."'
			),";
		}
		$SettingsQuery=substr($SettingsQuery, 0, -1);
	}else
		$SettingsQuery=0;

	return $SettingsQuery;
}
?>